<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<%@include file="/quick/include/commonQuery.jsp"%>
<%@ include file="/common/taglibs.jsp" %>
<c:set var="buttons">
  <c:if test='${model.parameter.STATUS=="N"}'>
    <c:if test='${ empty model.parameter.flag}'>
	    <input type="button" style="margin-right: 5px"   onclick="f_add()" value="<fmt:message key="button.add"/>"/>
	    <input type="button" style="margin-right: 5px"   onclick="f_delete()" value="<fmt:message key="button.delete"/>"/>
	    <input type="button" style="margin-right: 5px"   onclick="f_cancel()" value="<fmt:message key="button.cancel"/>"/>
	    <input type="button" style="margin-right: 5px"   onclick="f_save()" value="<fmt:message key="button.save"/>"/>
    </c:if>
    <input type="button" style="margin-right: 5px"   onclick="f_query()" value="<fmt:message key="button.query"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_input()" value="<fmt:message key="button.score.input"/>"/>
</c:if>

  <c:if test='${model.parameter.STATUS=="Y"}'>
    <input type="button" style="margin-right: 5px"   onclick="f_query()" value="<fmt:message key="button.query"/>"/>
    <input type="button" style="margin-right: 5px"   onclick="f_scan()" value="<fmt:message key="button.score.scan"/>"/>
</c:if>

</c:set>
<div id='toolbar' style="display:none">
<c:out value="${buttons}" escapeXml="false"/>
</div>
<div id='school_year_semester_form_div'></div>
<script>
Ext.onReady(globalScope._ready);


var currentSchoolYear="<c:out value='${model.parameter.SCHOOL_YEAR}'/>";
var currentSchoolSemester="<c:out value='${model.parameter.SCHOOL_SEMESTER}'/>";
var userAccount="<c:out value='${model.parameter.INPUTOR_ACCOUNT}'/>";
var userName='<authz:authentication operation="firstName"/>';
var scoreType='<c:out value="${model.parameter.SCORE_TYPE}"/>';//01-选课成绩,02-重修成绩,03-补考成绩,04,手工录入成绩

if(scoreType=='01'){
  document.title='成绩录入--<fmt:message key="webapp.name"/>';
}else if(scoreType=='02'){
  document.title='重修成绩录入--<fmt:message key="webapp.name"/>';  
}else if(scoreType=='03'){
  document.title='补考成绩录入--<fmt:message key="webapp.name"/>';  
}else if(scoreType=='04'){
  document.title='手工录入成绩--<fmt:message key="webapp.name"/>';  
}

Ext.onReady(function(){
  if(scoreType!='04'&&scoreType!='02') return;
  var grid=globalScope.grid.get("<c:out value='${model.tableName}'/>");
  var cm=grid.getColumnModel();
  var cols=cm.getColumnsBy(function(c,i){
		if(c.dataIndex=='COURSE_NATURE'||c.dataIndex=='EXAM_TYPE'||c.dataIndex=='CLASS_NO'){
			c.editable=true;
		}
  });	
  grid.reconfigure(grid.getStore(),cm);
});

  
var yearCombo,semesterCombo;
Ext.onReady(function(){
  Ext.get('title').dom.style.display='none';
  Ext.get('mainGrid').dom.style.border='0px solid #999';
  var yearStock=new Ext.data.Store({//学年度数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/findCodes.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'vcode'},
      {name:'vname'}
    ]) 
  });
  yearStock.load({params:{categoryCode:'SCHOOL_YEAR'}});
  
  yearCombo=new Ext.form.ComboBox({//学年度下拉框
    id:'_yearCombo',
    store:yearStock,
    fieldLabel:'学年度',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'vcode',
    displayField:'vname',
    readOnly:true,
    value:currentSchoolYear
  });
  
  var semesterStore=new Ext.data.Store({//学期数据源
    proxy:new Ext.data.HttpProxy({url:g_rootPath+"ajax/findCodes.html"}),
    reader:new Ext.data.ArrayReader({},[
      {name:'vcode'},
      {name:'vname'}
    ])
  });
  semesterStore.load({params:{categoryCode:'SCHOOL_SEMESTER'}});
  
  semesterCombo=new Ext.form.ComboBox({//学期下拉框
    store:semesterStore,
    fieldLabel:'学期',
    emptyText:'请选择',
    mode:'local',
    triggerAction:'all',
    valueField:'vcode',
    displayField:'vname',
    value:currentSchoolSemester,
    readOnly:true
  });
  

  var yearSemesterForm=new Ext.form.FormPanel({
    el:'school_year_semester_form_div',
    labelAlign:'right',
    labelWidth:60,
    frame:true,
    border:true,
//    title:'库存产品',
    height:40,
    items:[{
      layout:'column',
      items:[{
          columnWidth:1/2,
          layout:'form',
          items:[yearCombo]
        },{
          columnWidth:1/2,
          layout:'column',
          items:[
            {
              columnWidth:1/2,
              layout:'form',
              items:[semesterCombo]
            },
            {
              columnWidth:1/2,
              layout:'form',
              items:[{xtype:'button',text:'确定',handler:_data}]
            }
          ]
        }
      ]
    }]
  });
  yearSemesterForm.render();
  
	function _data(){
      	currentSchoolYear=yearCombo.getValue();
      	currentSchoolSemester=semesterCombo.getValue();
	    globalScope._changeQuickCookie('SCHOOL_YEAR',currentSchoolYear);
	    globalScope._changeQuickCookie('SCHOOL_SEMESTER',currentSchoolSemester);
	    var cookieForm=Ext.get('cookieForm').dom;
	    
	    var scoreCourseTable='T_ORIGINAL_SCORE_COURSE';
	    globalScope._model({tableName:scoreCourseTable,INPUTOR_ACCOUNT:userAccount
	                         ,STATUS:'<c:out value="${model.parameter.STATUS}"/>'
	                         ,SCORE_TYPE:scoreType
	                         ,SCHOOL_YEAR:yearCombo.getValue(),SCHOOL_SEMESTER:semesterCombo.getValue()
	                       }
	      ,function(responseText){
	        var htmlDiv=Ext.get("table_html").dom;
		    htmlDiv.innerHTML=responseText;
		    var htmlTable=Ext.get(scoreCourseTable);
		    globalScope.grid.get(scoreCourseTable).reloadData(htmlTable);	
			var after_query_span=Ext.get('g_pageSpan_info_'+scoreCourseTable).dom;
			var after_query_param=Ext.decode(after_query_span.innerText)
			after_query_param=Ext.apply({spanName:'g_pageSpan_'+scoreCourseTable},after_query_param);
		    globalScope._pageSpan(after_query_param); 		      
	   },false);	
	}
});





function f_add(){
  var year=yearCombo.getValue();
  var semester=semesterCombo.getValue();
  var tableName='';
  var commonQueryCondition=null;
  switch(scoreType){
    case '01'://正常
      tableName='V_TASK';
      commonQueryCondition='input_score_conditon';
      break;
    case '02'://重修
      tableName='V_RESTUDY_COURSE';
      commonQueryCondition='input_restudy_score_conditon';
      break;
    case '03'://补考
      tableName='V_REEXAM_COURSE';
      commonQueryCondition='input_score_conditon';
      break;
    case '04'://手工
      tableName='T_COURSE';
      break;
  }
  f_commonQuery(tableName,function(recs){
	    Ext.each(recs,function(item){
		      var updtArray=[];
		      var commonQueryGrid=globalScope.grid.get("<c:out value='${model.tableName}'/>");
		      var companyDepartmentStore=commonQueryGrid.getStore();
			  var newRecord=commonQueryGrid.newRecord('newId');//新建Record
			  newRecord.copyData(item.data); //复制数据
			  newRecord.set('INPUTOR_ACCOUNT',userAccount);
			  newRecord.set('INPUTOR_NAME',userName);
			  newRecord.set('SCORE_FORMAT','02');//成绩形式默认为百分制
			  newRecord.set('STATUS','N');
			  newRecord.set('SCORE_TYPE',scoreType);
			  newRecord.set('SCHOOL_YEAR',year);
			  newRecord.set('SCHOOL_SEMESTER',semester);
			  if(Ext.isEmpty(newRecord.get('COURSE_NATURE'))){
			  	newRecord.set('COURSE_NATURE','');
			  }			  
			  if(Ext.isEmpty(newRecord.get('EXAM_TYPE'))){
			  	newRecord.set('EXAM_TYPE','');
			  }			  
			  if(Ext.isEmpty(newRecord.get('CLASS_NO'))){
			  	newRecord.set('CLASS_NO',newRecord.get('COURSE_NO'));
			  }			  
			  commonQueryGrid.stopEditing();//插入Record
	    	  companyDepartmentStore.insert(0,newRecord); 
	    	  commonQueryGrid.startEditing(0,0);
	    	  newRecord.dirty=true;
	    	  newRecord.modified=newRecord.data;
	    	  if(companyDepartmentStore.modified.indexOf(newRecord)==-1){
	    	    companyDepartmentStore.modified.push(newRecord);	
	    	  }	
	    });	
  },
	  function(params){
        Ext.apply(params,{SCHOOL_YEAR:year,SCHOOL_SEMESTER:semester,commonQueryCondition:commonQueryCondition,placeValues:"['"+year+"','"+semester+"','"+scoreType+"']"});	  
	  }); 

}

function f_update(tableName){
  globalScope._update(tableName);
}

function f_delete(tableName){
  globalScope._delete(tableName);
} 

function f_cancel(tableName){
  globalScope._cancel(tableName);
}

function f_save(tableName,url){
  globalScope._save(tableName,url);
}

function f_query(tableName){
    globalScope._query(tableName);
}
function f_input(){
  var r=globalScope.grid.get("T_ORIGINAL_SCORE_COURSE").getSelectionModel().getSelected();
  if(Ext.isEmpty(r)){
    Ext.Msg.alert('WARNING','请选择课程');
    return;
  }
  var id=r.id;
   var matrix=[["tableName","V_ORIGINAL_SCORE"],["REFID",id],["scoreType",scoreType],['schoolYear',currentSchoolYear],['schoolSemester',currentSchoolSemester]];
   var form=createForm("<c:url value='/originalScoreHandler/quick/quickWithParentInfo.html'/>",matrix);
   form.submit();
 }
function f_scan(){
  var r=globalScope.grid.get("T_ORIGINAL_SCORE_COURSE").getSelectionModel().getSelected();
  if(Ext.isEmpty(r)){
    Ext.Msg.alert('WARNING','请选择课程');
    return;
  }
  var id=r.id;
   var matrix=[["tableName","V_ORIGINAL_SCORE_READ"],["REFID",id],["scoreType",scoreType],['schoolYear',currentSchoolYear],['schoolSemester',currentSchoolSemester]];
   var form=createForm("<c:url value='/originalScoreHandler/quick/quickWithParentInfo.html'/>",matrix);
   form.submit();
 }


function f_gotoPage(pageNo,rowsOfPage,pageCount,rowCount,conditoin){
  globalScope._gotoPage({spanName:"g_pageSpan_<c:out value='${model.tableName}'/>",
                         pageNo:pageNo,rowsOfPage:rowsOfPage,pageCount:pageCount,rowCount:rowCount,
                         condition:conditoin,tableName:"<c:out value='${model.tableName}'/>"
                       },{
                              INPUTOR_ACCOUNT:userAccount
	                         ,STATUS:'<c:out value="${model.parameter.STATUS}"/>'
	                         ,SCORE_TYPE:scoreType
	                         ,SCHOOL_YEAR:yearCombo.getValue(),SCHOOL_SEMESTER:semesterCombo.getValue()
                       });
}
</script>
